<template>
  <view class="flex flex-col min-h-500rpx m-24rpx bg-white rounded-32rpx p-24rpx">
    <view class="h-54rpx leading-54rpx text-30rpx font-semibold">审批流程</view>
    <view>
      <view>
        <u-steps
          :list="list"
          direction="column"
          :current="current"
          :assist="workData?.can_assist"
          @onAssist="onAssist"></u-steps>
      </view>
    </view>
  </view>
  <footVue
    :data="props.footData"
    @onLoad="loadReset"
    @change="onSave"
    @reset="onReset"
    :amend="props.amend"
    :form="props.form"
    :module="props.module"
    :workData="workData"
    @onSave="onSave"></footVue>
</template>

<script setup lang="ts">
import USteps from '@/uni_modules/vk-uview-ui/components/u-steps/u-steps.vue'
import { req } from '@/api/req'
import footVue from './foot'
import { onLoad, onShow } from '@dcloudio/uni-app'

const loading = ref(false)
const id = ref<any>(null)
const props = withDefaults(
  defineProps<{
    module?: string | null
    modelValue?: Record<string, any>
    form?: Record<string, any>
    //人事tab
    footData?: Record<string, any>
    //节点条件
    params?: Record<string, any>
    //回退到提交人编辑
    amend?: boolean
  }>(),
  {
    amend: false,
  }
)
const amend = ref(false)

const emits = defineEmits<{
  (event: 'update:modelValue', value: Record<string, any>): void
  (event: 'onReset', value: string): void
  (event: 'onSave', value: string): void
  (event: 'loadReset', value: string): void
}>()
const ids = ref<any>(null)
const current = ref<number>(1)
const list = ref<Record<string, any>[]>([])
const workData = ref<Record<string, any>>()

async function load() {
  if (loading.value) return
  loading.value = true

  await nextTick
  let info = {
    module: props.module,
    id: id.value || props.form?.id,
  }

  if (props.module == 'staff_leave') {
    info.duration = props.form?.duration || ''
  }
  if (props.module == 'staff_clock_in_adjustment') {
    info.certifier_belong_ids = props.form?.certifier_belong_ids || ''
  }
  const res = await req('Workflow/get_workflow', info)
  if (res.data.can_submit == '1' && props.amend) {
    amend.value = true
  } else {
    amend.value = false
  }
  list.value = res.data.node_data
  workData.value = res.data

  list.value.map((e) => {
    e.status == 1 ? (e.color = '#E2F0FF') : ''
    e.status == 2 ? (e.color = '#2979ff') : ''
    e.status == 3 ? (e.color = '#ff4449') : ''
  })
  const statusList = res.data.node_data.map((e: any) => e.status)
  if (statusList.every((e) => e == 2)) {
    return (current.value = statusList.length - 1)
  } else if (statusList.some((e) => e == 3)) {
    current.value = statusList.lastIndexOf('3')
  } else {
    current.value = statusList.lastIndexOf('2')
  }
  loading.value = false
}

function onAssist() {
  uni.navigateTo({
    url: `/pages/audit/assist?id=${props.form?.id}&module=${props.module}`,
  })
}

watch(
  props,
  () => {
    // if(props.module=='staff_leave'){
    //   props.form?.duration!=''?load():''
    // }
  },
  {
    deep: true,
    immediate: true,
  }
)

function loadReset() {
  load()
  emits('loadReset', '')
}

function onReset() {
  emits('onReset', '1')
}

function onSave(is_draft: string) {
  emits('onSave', is_draft)
}

onMounted(() => {
  // load()
})

onLoad((query) => {
  query?.id ? (id.value = query.id) : ''
})

function getData() {
  return workData.value
}

defineExpose({
  load,
  getData,
})
</script>

<style scoped></style>
